package dongshi.daddy.adaptive.useinsubcls;

import com.alibaba.dubbo.common.extension.Adaptive;
import com.alibaba.dubbo.common.extension.ExtensionLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Adaptive
public class AdaptiveUseInSubClsInterface implements UseInSubClsInterface {
    // 封装所有的扩展类的集合
    private final List<UseInSubClsInterface> factories;

    public AdaptiveUseInSubClsInterface() {
        // 获取接口UseInSubClsInterface的所有的扩展类
        ExtensionLoader<UseInSubClsInterface> loader
                = ExtensionLoader.getExtensionLoader(UseInSubClsInterface.class);
        List<UseInSubClsInterface> list = new ArrayList<UseInSubClsInterface>();
        for (String name : loader.getSupportedExtensions()) {
            list.add(loader.getExtension(name));
        }
        factories = Collections.unmodifiableList(list);
    }
    @Override
    public void sayHi(String word) {
        // 任选一个扩展类来调用其方法
        int size = factories.size();
        Random random = new Random();
        factories.get(random.nextInt(size)).sayHi(word);
    }
}
